package com.example.saveqqpassword;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
//import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
//
//import org.json.JSONException;
//import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    EditText myUsername,myPassword;
    Button buttonLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myUsername = (EditText)findViewById(R.id.et_number);
        myPassword = (EditText)findViewById(R.id.et_password);
        buttonLogin = (Button)findViewById(R.id.btn_login);

        Map<String, String> userInfo = null;
//        try {
//            InputStream is = this.openFileInput("Info.json");
//            userInfo = SaveInfobyJson.getQQInfo(this,is);
//        }catch (IOException e){
//            System.out.print("文件打开错误");
//        }
        userInfo = SaveQQInfo.getQQInfo(this);
        if(userInfo !=null) {//第一次文件不存在
            myUsername.setText(userInfo.get("username"));
            myPassword.setText(userInfo.get("password"));
        }

        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = myUsername.getText().toString();
                String password = myPassword.getText().toString();
                Boolean isSave=false;
//                try {
//                    FileOutputStream out = MainActivity.this.openFileOutput("Info.json", MODE_PRIVATE);
//                    isSave = SaveInfobyJson.saveQQInfo(MainActivity.this,username,password,out);
//                }catch (FileNotFoundException e) {
//                    Log.v("life","创建文件失败");
//                }catch(JSONException e){
//                    Log.v("life","JSON异常");
//                }
                isSave = SaveQQInfo.saveQQInfo(MainActivity.this,username,password);
                if(isSave){
                    Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
